package com.innovation.chengzhangriji.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.BabyQRCodeGetResponse;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteTextView;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class BabyQRCodeDialogActivity extends BaseActivity implements CuteToolbar.OnClickCuteToolbarListener {

    private ImageView mImageQRCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showAsPopup(this);
        setupLayout();
    }

    private void showAsPopup(Activity activity) {
        activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.width = dm.widthPixels - (int) (48 * 2 * dm.density);
        params.alpha = 1.0f;
        params.dimAmount = 0.6f;
        activity.getWindow().setAttributes(params);
    }

    private void setupLayout() {
        setContentView(R.layout.activity_baby_qrcode_dialog);
        String url = getIntent().getStringExtra("url");
        String name = getIntent().getStringExtra("name");
        String day = getIntent().getStringExtra("day");
        CuteToolbar toolbar = (CuteToolbar) findViewById(R.id.toolbar);
        toolbar.setOnClickCuteToolbarListener(this);
        RoundedImageView imageHead = (RoundedImageView) findViewById(R.id.image_head);
        ImageLoader.getInstance().displayImage(url, imageHead, Config.getOptionsHeadUnKnown());
        mImageQRCode = (ImageView) findViewById(R.id.image_qrcode);
        CuteTextView textName = (CuteTextView) findViewById(R.id.text_name);
        textName.setText(name);
        CuteTextView textDay = (CuteTextView) findViewById(R.id.text_day);
        textDay.setText(day);

        LoginInfo loginInfo = Config.getLoginInfo(this);
        String phone = loginInfo.getPhone();
        String token = loginInfo.getToken().getAccessToken();
        int id = loginInfo.getBabyID();
        Client.requestBabyQRCodeGet(this, phone, token, id, new ResponseHandler() {

            @Override
            public void onInnovationSuccess(JsonElement value) {
                BabyQRCodeGetResponse response = get(value.toString(),BabyQRCodeGetResponse.class);
                ImageLoader.getInstance().displayImage(response.getQrCodeUrl(),mImageQRCode);
            }
        }, networkUnavailableSampleImpl());
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

    }
}
